package cn.kgc.mes.routeproduct.business;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import cn.kgc.mes.common.utils.StringUtils;
import org.springframework.stereotype.Component;
import cn.kgc.mes.common.core.plus.dto.common.CommonDataPageResponse;
import cn.kgc.mes.common.core.plus.dto.page.PageDtoRequest;
import cn.kgc.mes.common.core.plus.dto.page.PageDtoResponse;
import cn.kgc.mes.common.core.plus.response.StatusResponse;
import org.springframework.beans.BeanUtils;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import cn.kgc.mes.routeproduct.domain.vo.RouteProductVo;
import cn.kgc.mes.routeproduct.domain.RouteProduct;
import cn.kgc.mes.routeproduct.service.RouteProductService;

/**
 * 产品制程业务
 * 
 * @author ruoyi
 * @date 2025-09-14
 */
@Component
public class RouteProductBusiness {

    @Resource
    RouteProductService settingService;


    /**
     * 分页
     *
     * @param vo
     * @return
     */
    public CommonDataPageResponse<RouteProductVo> page(RouteProductVo vo) {
        CommonDataPageResponse<RouteProductVo> response = new CommonDataPageResponse<>();

        //分页
        Long current = 1L;
        Long size = 10L;

        PageDtoRequest reqPageDto = vo.getPageDto();
        if (reqPageDto != null) {
            current = reqPageDto.getCurrent() != null ? reqPageDto.getCurrent() : 1L;
            size = reqPageDto.getSize() != null ? reqPageDto.getSize() : 10L;
        }

        Page page = new Page<>(current, size);

        LambdaQueryWrapper<RouteProduct> queryWrapper = Wrappers.lambdaQuery();

        queryWrapper.eq(vo.getRouteId() != null, RouteProduct::getRouteId, vo.getRouteId());

        queryWrapper.eq(vo.getItemId() != null, RouteProduct::getItemId, vo.getItemId());

        queryWrapper.eq(StringUtils.hasText(vo.getItemCode()), RouteProduct::getItemCode, vo.getItemCode());

        queryWrapper.like(StringUtils.hasText(vo.getItemName()), RouteProduct::getItemName, vo.getItemName());

        queryWrapper.eq(StringUtils.hasText(vo.getSpecification()), RouteProduct::getSpecification, vo.getSpecification());

        queryWrapper.eq(StringUtils.hasText(vo.getUnitOfMeasure()), RouteProduct::getUnitOfMeasure, vo.getUnitOfMeasure());

        queryWrapper.eq(vo.getQuantity() != null, RouteProduct::getQuantity, vo.getQuantity());

        queryWrapper.eq(StringUtils.hasText(vo.getTimeUnitType()), RouteProduct::getTimeUnitType, vo.getTimeUnitType());


        queryWrapper.orderByDesc(RouteProduct::getRecordId);

        IPage<RouteProduct> pageRecords = settingService.page(page, queryWrapper);

        //分页记录
        List<RouteProduct> records = pageRecords.getRecords();
        //转为vo
        List<RouteProductVo> voList = new ArrayList<>();
        for (RouteProduct record : records) {
            RouteProductVo settingVo  = new RouteProductVo();

            BeanUtils.copyProperties(record, settingVo);

            voList.add(settingVo);
        }
        response.setRecords(voList);

        //分页信息
        PageDtoResponse pageDto = new PageDtoResponse();
        pageDto.setCurrent(current);
        pageDto.setSize(size);
        pageDto.setTotal(pageRecords.getTotal());
        pageDto.setPages(pageRecords.getPages());

        response.setPageDto(pageDto);

        return response;
    }

    /**
     * 信息
     *
     * @param vo
     * @return
     */
    public RouteProductVo info(RouteProductVo vo) {

        RouteProductVo settingVo = new RouteProductVo();

        Long recordId = vo.getRecordId();

        RouteProduct record = settingService.getById(recordId);

        BeanUtils.copyProperties(record, settingVo);

        return settingVo;
    }

    /**
     * 添加
     *
     * @param vo
     * @return
     */
    public StatusResponse add(RouteProductVo vo) {
        StatusResponse response = new StatusResponse();

        RouteProduct record = new RouteProduct();

        BeanUtils.copyProperties(vo, record);

        settingService.save(record);

        response.setSuccess(true);

        return response;
    }

    /**
     * 更新
     *
     * @param vo
     * @return
     */
    public StatusResponse update(RouteProductVo vo) {
        StatusResponse response = new StatusResponse();

        RouteProduct record = new RouteProduct();

        BeanUtils.copyProperties(vo, record);

        settingService.updateById(record);

        response.setSuccess(true);

        return response;
    }

    /**
     * 删除
     *
     * @param vo
     * @return
     */
    public StatusResponse delete(RouteProductVo vo) {
        StatusResponse response = new StatusResponse();

        Long recordId = vo.getRecordId();

        settingService.removeById(recordId);

        response.setSuccess(true);

        return response;
    }

}
